#coding=utf-8
from global_utils import http_protocol
from  global_utils.log import logger
import json

class GetContexVar(object):
    def __init__(self,methon,url,header,parameter,context_var):
        self.methon=methon
        self.url=url
        self.headers=header
        self.parameter=parameter
        self.keys_list=eval(context_var)
    
    
    def get_response_josn(self):
        http=http_protocol.HttpHelp()
        if self.methon == "GET":        
            data = http.get(self.url, self.headers, self.parameter)    
        elif self.methon == "POST":
            data = http.post(self.url, self.headers, self.parameter)
        return data
        
    def get_dict_value(self, default=None):
            #default=None，在key值不存在的情况下，返回None
        data1=self.get_response_josn()
        if isinstance(data1,dict):
            #如果传入的数据为字典
            dictionary = dict(data1)
            #初始化字典
        else:
            dictionary = dict(eval(data1))
        for i in self.keys_list:
                    #按照keys_list顺序循环键值
            try:
                if dictionary.get(i) != None:
                    dict_values = dictionary.get(i)
                        #如果键对应的值不为空，返回对应的值
                elif dictionary.get(i) == None:
                    dict_values = dictionary.get(int(i))
                        #如果键对应的值为空，将字符串型的键转换为整数型，返回对应的值
            except:
                return default
                            #如果字符串型的键转换整数型错误，返回None
            dictionary = dict_values
        return dictionary

if __name__=="__main__":
    aaa=GetContexVar("POST","http://10.100.112.42:8083/booking/bookingService/getSN",{"Content-type": "application/json"},json.dumps({"memberId":"181297774"}),'["data","sn"]')
    print aaa.get_dict_value()
    